package com.freshcity.page.home
{
	import com.freshcity.ILayer;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.Window;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.page.common.HelpIcon;
	import com.freshcity.page.common.PagingIcon;
	import com.freshcity.tree.WaitConfirmTaskWin;
	import com.freshcity.tree.WaitDoneTaskWin;
	import com.freshcity.utils.MessageUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class TaskIcon extends Window
	{
		public static const ITASK_ACCEPT:int = 0;
		public static const ITASK_CONFIRM:int = 1;
		
		private var data:Array;
		private var _curContent:*;
		private var waitDoneBack:WaitDoneTaskWin;
		private var waitConfirmBack:WaitConfirmTaskWin;
		private var back:Sprite;
		private var paging:PagingIcon;
		private var index:int;

		public function TaskIcon(data:Array, parent:ILayer=null, xpos:Number=0, ypos:Number=0)
		{
			waitDoneBack=new WaitDoneTaskWin();
			var s1:String="在对方确认交互任务完成前，你的梧桐树相关功能将暂时锁定。超过最大时间5天后，将自动解锁，但要损失个人美誉值。";
			new HelpIcon("不完成任务有什么后果？",s1,waitDoneBack,15,97);
			var s2:String="尽快联系对方确认任务完成。梧桐树功能解锁以对方确认完成为准，所以在完成任务后，尽快要对方知道非常重要。";
			new HelpIcon("已经完成任务，下一步怎么做？",s2,waitDoneBack,15,116);
			waitDoneBack.closeBtn2.addEventListener(MouseEvent.CLICK, hide);

			waitConfirmBack=new WaitConfirmTaskWin();
			var s3:String="TA的梧桐树功能将一直保持锁定，直到超过最大期限。你的不积极回应，会极大影响TA对你的好感。";
			new HelpIcon("不确认任务状态对TA有什么影响？",s3,waitConfirmBack,15,97);
			waitConfirmBack.closeBtn2.addEventListener(MouseEvent.CLICK, hide);
			waitConfirmBack.submitBtn.addEventListener(MouseEvent.CLICK, confirmClick);

			this.data=data;
			super(parent, xpos, ypos);
			this.curContent = data[index];
			this.mouseEnabled=false;
			this.title = "交互任务";
			this.hasCloseButton = true;
			this.setSize(380,220);
			
			paging.addEventListener(EventName.SwitchPage, onSwitchPage);
		}

		protected function onSwitchPage(event:DataEvent):void
		{
			var dire:String=event.data as String;
			if (dire == "forward")
			{
				index--;
			}
			else
			{
				index++;
			}
			this.curContent=data[index];
		}

		protected function confirmClick(event:MouseEvent):void
		{
			var taskId:String = this.curContent.id;
			var user:String = this.curContent.t;
			
			main.commu.send(OpCodes.CONFIRM_ITASK, {u:user,t:taskId}, confirmTaskCallback);
		}
		
		private function confirmTaskCallback(data:*):void
		{
			this.hide();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			paging=new PagingIcon(this.data.length);
			addChild(paging);

			paging.x=312;
			paging.y=159;
		}

		override public function destroy():void
		{
			super.destroy();
		}

		public function get curContent():*
		{
			return _curContent;
		}

		public function set curContent(value:*):void
		{
			_curContent=value;
			if (back)
				this.content.removeChild(back);
			if (_curContent.s == TaskIcon.ITASK_ACCEPT)
			{
				waitDoneBack.taskContent.text=MessageUtil.decodeITask(_curContent.d,_curContent.t,_curContent.s);
				back=waitDoneBack;
			}
			else
			{
				waitConfirmBack.taskContent.text=MessageUtil.decodeITask(_curContent.d,_curContent.t,_curContent.s);
				back=waitConfirmBack;
			}
			this.content.addChildAt(back,0);
		}
		
		override public function show(event:Event=null):void
		{
			if(!this.parent){
				this.layer = LayerManager.popupLayer;
				this.layer.addPopUp(this);
			}
			super.show(event);
		}

	}
}
